package com.polaris.live.ui.live.widget.adapter

import android.text.TextUtils
import android.view.View
import androidx.lifecycle.LifecycleCoroutineScope
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R
import com.polaris.live.adapter.live.LiveMessageAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.util.loadIfPresent
import com.polaris.live.databinding.LiveMessagePublicItemBinding
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.im.sfs.bean.live.ext.MessageUserExtBean
import com.polaris.live.im.sfs.bean.live.payload.PublicScreenPayloadBean
import com.polaris.live.ui.message.widget.single_message.AbstractLiveMessageItemHolder
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.RichCharmImgUtil
import com.polaris.live.utils.viewscope.autoDisposeScope
import com.polaris.live.widget.avatar.FrameImageUtils
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * LiveMessagePublicItem
 *
 * @author Created by 半仙 on 2023/9/14/014 16:53
 */
class LiveMessagePublicItem(
    adapter: LiveMessageAdapter,
    binding: LiveMessagePublicItemBinding,
    private val lifecycleScope: LifecycleCoroutineScope?
) : AbstractLiveMessageItemHolder<LiveMessagePublicItemBinding>(adapter, binding) {

    override fun bind(item: SendLiveMessage) {
        mBinding.apply {
            val ext = item.ext as? MessageUserExtBean?
            val payload = item.payload as? PublicScreenPayloadBean?

            avatarImg.setImageUrl(ext?.user?.avatar, ext?.user?.userId)
            avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(ext?.user?.avatarFrame))

            CommonUtils.setNumberMedal(lifecycleScope, ext?.user?.numberMedal, mBinding.pagImageMe, mBinding.ivImageMe)
            vipView.setVipLevel(ext?.user?.vipLevel)
            loadIfPresent({ item.span1 }, {
                mBinding.root.autoDisposeScope.launch {
                    try {
                        val span = CommonUtils.setSpanData(liveHead, item, mAdapter.context)
                        withContext(CoroutineScopeManager.mainDispatcher) {
                            emit(span.create())
                        }
                    } catch (_: Exception) {
                    }
                }
            }, {
                liveHead.text = it
                item.span1 = it
            })
            val color =
                RichCharmImgUtil.serColor2HexColor(ext?.user?.messageTextColor) ?: ColorUtils.getColor(R.color.white)
            if (!CollectionUtils.isEmpty(ext?.atUsers)) {
                val string = StringBuilder()

                ext?.atUsers?.forEach {
                    string.append("@${it.nickname}")
                }
                loadIfPresent({ item.span2 }, {
                    val span = SpanUtils.with(content)
                        .append(string)
                        .setForegroundColor(ColorUtils.getColor(R.color.fff_70))
                        .append(item.content)
                        .setForegroundColor(color)
                        .create()

                    emit(span)
                }, {
                    content.text = it
                    item.span2 = it
                })
            } else {
                content.text = item.content
                content.setTextColor(color)
            }
            translateContent.setTextColor(color)
            if (TextUtils.isEmpty(payload?.translateContent)) {
                translateContent.visibility = View.GONE
            } else {
                if (StringUtils.equals(payload?.translateContent, item.content)) {
                    translateContent.visibility = View.GONE
                } else {
                    translateContent.text = payload?.translateContent
                    translateContent.visibility = View.VISIBLE
                }
            }

            if (ext?.user?.userId == UserManager.getUser().userId) {
                translateContent.visibility = View.GONE
            }
        }
    }
}